<template>
	<view>
		<cu-custom :isBack="true" bgColor="bg-gradual-blue">
			<block slot="content">信息设置</block>
		</cu-custom>
		
		<view class="cu-list menu sm-border card-menu margin-top" >
			
			<view class="cu-item" >
				<view class="content">
					<text>头像</text>
				</view>
				<view class="action">
					<button style="width: 45px;height:45px;margin:0px;padding: 0px;border-radius:50%;" type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseavatar">
						<image style="width: 100%;height:100%;border-radius:50%;" mode="scaleToFill" :src="userinfo.avatar"></image>
					</button>				
				</view>
			</view>
			
			<view class="cu-item" >
				<view class="content">
					<text>昵称</text>
				</view>
				<view class="action">
					<input class="text-right" placeholder="请输入您的昵称" name="input" type="nickname" @change="changeName" v-model="userinfo.name"></input>
				</view>
			</view>
			
			<view class="cu-item arrow">
				<view class="content">
					<text>权限设置</text>
				</view>
				<view class="action" @tap="goConfig">
					<text class="text-grey">点击管理</text>
				</view>
			</view>
						
			<view class="cu-item arrow" >
				<view class="content">
					<text>用户协议</text>
				</view>
				<view class="action" @tap="openAgreement">
					<text class="text-grey">点击查看</text>
				</view>
			</view>
			
			<view class="cu-item arrow" >
				<view class="content">
					<text>隐私政策</text>
				</view>
				<view class="action" @tap="openPrivacy">
					<text class="text-grey">点击查看</text>
				</view>
			</view>
			
			<view class="cu-item arrow">
				<view class="content">
					<text>意见建议</text>
				</view>
				<view class="action" @tap="goSuggestion">
					<text class="text-grey">点击查看</text>
				</view>
			</view>
			
		</view>
		
		<view class="padding flex flex-direction">
			<button class="cu-btn round bg-red margin-tb-sm lg" @tap="change">保存修改</button>
			<button class="cu-btn round line-red margin-tb-sm lg" @tap="goMy">返回退出</button>
		</view>
		
	</view>
</template>

<script>
		import * as confirm from '@/common/confirm.js'
		import * as upload from '@/common/upload.js'
		
		import {mapGetters} from 'vuex'
		
		export default {
		data() {
			return {
				//状态数据
				userinfo:{name:'微信用户'},//用户信息
			}
		},
		created(){
			this.$api.post('/user/ckIsAuth').then(res => {
				this.userinfo=res.data
			})
		},
		methods:{
			goConfig(){
				uni.openSetting({
				  success(res) {
				    console.log(res.authSetting)
				  }
				});
			},
			goSuggestion(){
				uni.navigateTo({
					url:"/pages/my/suggestion/suggestion_list"
				})
			},
			//修改用户信息
			changeInfo(){
				if(this.edit_user_info.name.trim()==''){
					confirm.error("请设置昵称!")
				}else{					
					// 上传图片
					let _this=this
					
					/* 文件上传部分开始 */
					if(_this.edit_user_info.avatar.indexOf('wxfile://')>-1){
						this.$api.upload('/Upload/Upload',_this.edit_user_info.avatarUrl,{}).then((res)=>{
							_this.edit_user_info.avatar=res.data
							_this.updateUserInfo()
						}).catch(()=>{
							confirm.error("文件上传错误!")
						})
					}else{
						_this.edit_user_info.avatar=_this.edit_user_info.avatarUrl
						_this.updateUserInfo()
					}
					/* 文件上传部分结束 */
				}
			},
			changeName(e){
				this.userinfo.name=e.target.value
			},
			openAgreement(){
				this.openPdf('https://smart1st.online/admin/202412/agreement.pdf');
			},
			openPrivacy(){
				this.openPdf('https://smart1st.online/admin/202412/privacy.pdf');
			},
			openPdf(url) {
			  uni.downloadFile({
			    url: url, // PDF文件的网络地址
			    success: (res) => {
			      if (res.statusCode === 200) {
			        let filePath = res.tempFilePath;
			        // 根据不同的平台可能需要将路径转换为平台可识别的路径
			        // 例如，在App端可能需要使用plus.io.convertLocalFileSystemURL
			        uni.openDocument({
			          filePath: filePath,
			          fileType: 'pdf', // 明确指定文件类型为pdf
			          success: function (res) {
			            console.log('打开PDF成功');
			          },
			          fail: function (err) {
			            console.error('打开PDF失败', err);
			          }
			        });
			      }
			    }
			  });
			},
			goMy(){
				uni.navigateBack()
			},
			change(){
				this.$api.post('/user/change_nickname',{name:this.userinfo.name}).then(res => {
					confirm.success("修改信息成功!!",()=>{
						this.showUserinfo=false
						uni.reLaunch({
							url:'/pages/login/login'
						})
					})	
				}).catch((err)=>{
					uni.showToast({
						title:err.msg,
						icon:'none'
					})
				})
			},
			onChooseavatar(e){
				let avatarUrl = e.detail.avatarUrl
				let _this=this
				upload.uploadImg('/user/upload_avatar',avatarUrl,function(res){
					uni.showToast({
						title:"修改头像成功"
					})
					_this.userinfo.avatar=res
				},{})
			}
		}
	};
</script>

<style>
</style>